/* Copyright (C) 2008, 2009, 2010 Faindu GmbH, Switzerland.
* All Rights Reserved. The following is Source Code and is subject to all restrictions 
* on such code as contained in the license accompanying this project. */
package blocstac.editimage.base
{

import blocstac.editimage.EditState;
import blocstac.util.Print;

import flash.display.BitmapData;

import flexunit.framework.TestCase;

public class ViewModelTest extends TestCase
{

    //==========================================================================
    //
    //  Test set up and tear down
    //
    //--------------------------------------------------------------------------  
	private var viewModel:ViewModel
	private var imageModel:ImageModel
	private var imageData:BitmapData
	
	override public function setUp(): void
	{
		imageModel = new ImageModel()
		viewModel = new ViewModel(imageModel)
		imageData = new BitmapData(10, 15)
	}
	
	override public function tearDown():void
	{
	}
	
    //==========================================================================
    //
    //  Tests
    //
    //--------------------------------------------------------------------------  
	[Test]
	public function test_interactionStarts(): void
	{
		viewModel.switchToEditState(EditState.editable)
		assertTrue(viewModel.interactionStarts())
		assertFalse(viewModel.interactionStarts())
		
		viewModel.interactionEnds()
		assertTrue(viewModel.interactionStarts())
	}

	[Test]
	public function test_interactionStarts_but_not_editable(): void
	{
		assertFalse(viewModel.interactionStarts())
	}

	[Test]
	public function test_imageData(): void
	{
		imageModel.updateImageData(imageData)
		assertTrue(imageModel.imageData === viewModel.imageData)
	}

	[Test]
	public function test_imageDataChanged(): void
	{	
		assertFalse(viewModel.imageDataChanged)
		imageModel.updateImageData(imageData)
		assertTrue(viewModel.imageDataChanged)
	}

	[Test]
	public function test_resetImageDataChanged(): void
	{	
		assertFalse(viewModel.imageDataChanged)
		viewModel.resetImageDataChanged()
		assertFalse(viewModel.imageDataChanged)
		
		imageModel.updateImageData(imageData)
		assertTrue(viewModel.imageDataChanged)
		viewModel.resetImageDataChanged()
		assertFalse(viewModel.imageDataChanged)
	}
	
	[Test]
	public function test_angle(): void
	{
		assertEquals(0, viewModel.angle)
		imageModel.angle = 1.2
		assertEquals(1.2, viewModel.angle)
	}
		
	[Test]
	public function test_rotationMatrix(): void
	{	
		imageModel.updateImageData(imageData)
		imageModel.angle = Math.PI / 2 
		
		// Mac     assertEquals("(a=6.123233995736766e-17, b=1, c=-1, d=6.123233995736766e-17, tx=NaN, ty=NaN)", viewModel.rotationMatrix.toString())
		// Windows assertEquals("(a=6.123031769111886e-17, b=1, c=-1, d=6.123031769111886e-17, tx=NaN, ty=NaN)", viewModel.rotationMatrix.toString())
		assertEquals("[a=0, b=1, c=-1, d=0, tx=0, ty=0]", Print.matrix(viewModel.rotationMatrix, 8))
	}
	
	[Test]
	public function test_rotationMatrix_interactionEnds(): void
	{	
		imageModel.updateImageData(imageData)
		imageModel.angle = Math.PI / 2 
		viewModel.interactionEnds()
		
		// Mac     assertEquals("(a=6.123233995736766e-17, b=1, c=-1, d=6.123233995736766e-17, tx=15, ty=0)", viewModel.rotationMatrix.toString())
		// Windows assertEquals("(a=6.123031769111886e-17, b=1, c=-1, d=6.123031769111886e-17, tx=15, ty=0)", viewModel.rotationMatrix.toString())
		assertEquals("[a=0, b=1, c=-1, d=0, tx=15, ty=0]", Print.matrix(viewModel.rotationMatrix, 8))
	}

	[Test]
	public function test_imageCenterInImageComp(): void
	{	
		imageModel.editedScaleX = 2
		imageModel.editedScaleY = 3
		assertEquals(0, viewModel.imageCenterInImageComp.x)
		assertEquals(0, viewModel.imageCenterInImageComp.y)
		
		imageModel.updateImageData(imageData)
		assertEquals(10, viewModel.imageCenterInImageComp.x)
		assertEquals(22.5, viewModel.imageCenterInImageComp.y)
	}

	[Test]
	public function test_brImageCornerInRotationCompUnscaled(): void
	{	
		imageModel.editedScaleX = 2
		imageModel.editedScaleY = 3
		assertEquals(0, viewModel.brImageCornerInRotationCompUnscaled.x)
		assertEquals(0, viewModel.brImageCornerInRotationCompUnscaled.y)
		
		imageModel.updateImageData(imageData)
		assertEquals(10, viewModel.brImageCornerInRotationCompUnscaled.x)
		assertEquals(15, viewModel.brImageCornerInRotationCompUnscaled.y)
	}
	
	[Test]
	public function test_editedScale(): void
	{	
		assertEquals(1, viewModel.editedScaleX)
		assertEquals(1, viewModel.editedScaleY)
		
		imageModel.editedScaleX = 2
		imageModel.editedScaleY = 3
		assertEquals(2, viewModel.editedScaleX)
		assertEquals(3, viewModel.editedScaleY)
	}
	
	[Test]
	public function test_imageMatrix(): void
	{	
		assertEquals("(a=1, b=0, c=0, d=1, tx=0, ty=0)", viewModel.imageMatrix.toString())
		
		imageModel.editedScaleX = 2
		imageModel.editedScaleY = 3
		assertEquals("(a=2, b=0, c=0, d=3, tx=0, ty=0)", viewModel.imageMatrix.toString())
	}

}
}